package com.tarena.lbs.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.encoder.JwtEncoder;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.user.dao.mapper.AdminMapper;
import com.tarena.lbs.user.dao.mapper.UserMapper;
import com.tarena.lbs.user.pojo.param.AdminLoginParam;
import com.tarena.lbs.user.pojo.param.UserLoginParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.vo.LoginVO;
import com.tarena.lbs.user.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class  LoginServiceImpl implements LoginService {
    @Autowired
    private JwtEncoder jwtEncoder;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Long selectByPhone(String phone) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Phone", phone);
        Long num = userMapper.selectCount(queryWrapper);
        return num;
    }

    @Override
    public Long selectByPhoneAndPassword(UserLoginParam param) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Phone", param.getPhone())
                .eq("Password", param.getPassword());
        Long num = userMapper.selectCount(queryWrapper);
        return num;
    }

    @Override
    public LoginVO getJwt(UserLoginParam param) {
        try {
            LoginVO loginVO = new LoginVO();
            QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("Phone", param.getPhone())
                    .eq("Password", param.getPassword());
            UserPO userPO = userMapper.selectOne(queryWrapper);
            UserPrinciple userPrinciple = new UserPrinciple();
            userPrinciple.setId(userPO.getId());
            userPrinciple.setNickname(userPO.getNickName());
            userPrinciple.setRole(Roles.USER);
            String token = jwtEncoder.generateToken(userPrinciple);
            loginVO.setJwt(token);
            loginVO.setId(userPO.getId());
            loginVO.setNickname(userPO.getNickName());
            return loginVO;
        } catch (BusinessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public LoginVO login(AdminLoginParam param) throws BusinessException {
        LoginVO vo = new LoginVO();
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_phone", param.getPhone())
                .eq("account_password", param.getPassword());
        AdminPO po = adminMapper.selectOne(queryWrapper);
        if (po == null) {
            throw new BusinessException("-5", "用户不存在");
        }
        UserPrinciple principle = new UserPrinciple();
        principle.setId(po.getId());
        principle.setNickname(po.getNickname());
        //判断登录的是商家还是管理员
        //type  0 管理员 1 商家
        principle.setRole(po.getAccountType() == 0 ? Roles.ADMIN : Roles.SHOP);
        String jwt = jwtEncoder.generateToken(principle);
        vo.setJwt(jwt);
        vo.setId(po.getId());
        vo.setNickname(po.getNickname());
        return vo;
    }
}
